import Mock from "mockjs";

// 模拟猫咪列表数据
const catList = [
    { id: "01", kind: "波斯猫", listing: false, hot: 320, views: 565, creater: "管理员一", time: "2024-11-01" },
    { id: "02", kind: "英短", listing: false, hot: 956, views: 1256, creater: "管理员二", time: "2024-11-02" },
    { id: "03", kind: "暹罗猫", listing: true, hot: 564, views: 651, creater: "管理员三", time: "2024-11-03" },
    { id: "04", kind: "美短", listing: false, hot: 456, views: 449, creater: "管理员一", time: "2024-11-04" },
    { id: "05", kind: "孟加拉猫", listing: true, hot: 49, views: 498, creater: "管理员二", time: "2024-11-05" },
    { id: "06", kind: "苏格兰折耳猫", listing: true, hot: 156, views: 616, creater: "管理员三", time: "2024-11-06" },
    { id: "07", kind: "阿比西尼亚猫", listing: false, hot: 616, views: 300, creater: "管理员一", time: "2024-11-07" },
    { id: "08", kind: "布偶猫", listing: true, hot: 340, views: 480, creater: "管理员二", time: "2024-11-08" },
    { id: "09", kind: "加菲猫", listing: true, hot: 290, views: 650, creater: "管理员三", time: "2024-11-09" },
    { id: "10", kind: "暹罗猫", listing: false, hot: 310, views: 500, creater: "管理员一", time: "2024-11-10" },
    { id: "11", kind: "土耳其安哥拉猫", listing: true, hot: 405, views: 780, creater: "管理员二", time: "2024-11-11" },
    { id: "12", kind: "阿尔及利亚猫", listing: true, hot: 370, views: 550, creater: "管理员三", time: "2024-11-12" },
    { id: "13", kind: "俄罗斯蓝猫", listing: false, hot: 280, views: 440, creater: "管理员一", time: "2024-11-13" },
    { id: "14", kind: "加拿大无毛猫", listing: true, hot: 360, views: 670, creater: "管理员二", time: "2024-11-14" },
    { id: "15", kind: "美洲豹猫", listing: true, hot: 415, views: 820, creater: "管理员三", time: "2024-11-15" },
    { id: "16", kind: "缅因猫", listing: false, hot: 245, views: 390, creater: "管理员一", time: "2024-11-16" },
    { id: "17", kind: "俄罗斯蓝猫", listing: true, hot: 405, views: 730, creater: "管理员二", time: "2024-11-17" },
    { id: "18", kind: "雪鞋猫", listing: false, hot: 500, views: 980, creater: "管理员三", time: "2024-11-18" },
    { id: "19", kind: "日本短尾猫", listing: true, hot: 385, views: 600, creater: "管理员一", time: "2024-11-19" },
    { id: "20", kind: "土耳其猫", listing: true, hot: 345, views: 540, creater: "管理员二", time: "2024-11-20" }
];

// 获取猫咪列表接口
Mock.mock("/cat/list", "post", (config) => {
    const req = JSON.parse(config.body)
    return {
        code: 1,
        message: "获取猫咪列表成功",
        data: {
            catList: catList.slice((req.count - 1) * req.pageSize, req.count * req.pageSize),
            length: catList.length
        }
    };
});

// 添加猫咪接口
Mock.mock("/cat/add", "post", (config) => {
    const req = JSON.parse(config.body)
    catList.push(req)
    return {
        code: 200,
        message: '添加数据成功',
    }
});

// 删除猫咪接口
Mock.mock("/cat/delete", "post", (config) => {
    const req = JSON.parse(config.body)
    const res = {
        code: 200,
        message: '删除数据失败'
    }

    for (let i = 0; i < catList.length; i++) {
        if (catList[i].id == req.id) {
            catList.splice(i, 1)
            res.message = '数据删除成功'
            break
        }
    }

    return res
});

// 更新猫咪接口
Mock.mock("/cat/edit", "post", (config) => {
    const req = JSON.parse(config.body)
    const res = {
        code: 200,
        message: '修改数据失败'
    }

    for (let i = 0; i < catList.length; i++) {
        if (catList[i].id == req.id) {
            catList[i] = req
            res.message = '数据修改成功'
            break
        }
    }

    return res
});

// 查找猫咪接口
Mock.mock('/cat/query', 'post', (config) => {
    const req = JSON.parse(config.body);
    const res = {
        code: 200,
        message: '查询数据失败',
    };
    const arr = [];
    const kind = req.kind || ''; // 通过 req.kind 进行查询

    for (let i = 0; i < catList.length; i++) {
        // 确保 catList[i].kind 存在，并检查 req.kind 是否为空
        if (catList[i].kind && catList[i].kind.indexOf(kind) !== -1) {
            // 找到数据了
            res.message = '数据查询成功';
            arr.push(catList[i]);
        }
    }

    res.catList = arr;
    res.length = arr.length;
    return res;
});
